package com.xj.api.bussiness.news.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.news.service.impl.ITbNewsService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.news.entity.NewNews;
import com.xj.common.bussiness.news.entity.TbNews;

import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("news")
public class NewsController extends BaseController{
	
	@Autowired
	private ITbNewsService newsService;
	
	@GetMapping
	@ApiOperation(value = "内容列表", httpMethod = "GET", notes = "内容列表")
	public AbstractBean newsList(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize){
		return json(newsService.selectMapsPage(new Page<TbNews>(pageNum, pageSize), new EntityWrapper<TbNews>()));
	}
	
	@GetMapping("/{id}.content")
	@ApiOperation(value = "内容详情", httpMethod = "GET", notes = "内容详情")
	public AbstractBean newsContent(@PathVariable Integer id){
		return json(newsService.selectById(id));
	}
	
	/*
	@GetMapping("/{typeId}.list")
	@ApiOperation(value="类型查询" , httpMethod = "GET" , notes ="类型查询")
	public AbstractBean nameAndConverImgListByType(@PathVariable Integer typeId,@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize){
		return json(newsService.selectMapsPage(new Page<TbNews>(pageNum, pageSize),new EntityWrapper<TbNews>().eq("type_id", typeId)));
	}
	
	
	@GetMapping("/{id}.one")
	@ApiOperation(value="介绍查询" , httpMethod = "GET" , notes ="查询单个产品，找到前面id和后面id")
	public AbstractBean one(@PathVariable Integer id){
		OneAndNext oneAndNext = new OneAndNext();
		TbNews news = newsService.selectById(id);
		List<TbNews> idsList = newsService.idsList(news.getTypeId());
		oneAndNext.setId(id);
		oneAndNext.setKeyWords(news.getKeyWords());
		oneAndNext.setOtherImg(news.getOtherPhoto());
		oneAndNext.setOtherMovie(news.getOtherMovie());
		oneAndNext.setTitle(news.getTitle());
		oneAndNext.setTypeId(news.getTypeId());
		oneAndNext.setBeforeId(id);
		
		int index = 0 ;
		int max = idsList.size();
		for (int i = 0; i < idsList.size(); i++) {
			if (idsList.get(i).getId() ==id) {
				index = i;
			}
		}
		if (index != max -1) {
			oneAndNext.setNextId(idsList.get(index+1).getId());
		}else{
			oneAndNext.setNextId(idsList.get(max -1 ).getId());
		}
		
		return json(oneAndNext);
	}*/
	
	
	
	
	/*@GetMapping("/{id}.tree")
	@ApiOperation(value="图片查询" , httpMethod = "GET" , notes ="查询单个产品，找到前面id和后面id")
	public AbstractBean treeById(@PathVariable Integer id){
		TbNews news = newsService.selectById(id);
		
		List<TbNews> idsList = newsService.idsList(news.getTypeId());
		int index = 0 ;
		int max = idsList.size();
		String[] otherPhotos = null;
		for (int i = 0; i < idsList.size(); i++) {
			if (idsList.get(i).getId() ==id) {
				index = i;
			}
		}
		if (news.getOtherPhoto() != null) {
			otherPhotos = news.getOtherPhoto().split(";");
		}
		List<NewsVo> lists = new ArrayList<NewsVo>();
		NewsVo vo = new NewsVo();
		vo.setId(news.getId());
		vo.setImg(news.getCoverImg());
		vo.setIsShow(news.getIsShow());
		vo.setKeyWords(news.getKeyWords());
		vo.setMovie(news.getOtherMovie());
		vo.setSort(news.getSort());
		vo.setTitle(news.getTitle());
		vo.setTypeId(news.getTypeId());
		if (index != 0) {
			vo.setBeforeId(idsList.get(index-1).getId());
		}
		if (index != max -1) {
			vo.setNextId(idsList.get(index+1).getId());
		}else{
			vo.setNextId(idsList.get(max -1 ).getId());
		}
		lists.add(vo);
		if (otherPhotos != null) {
			for (int i = 0; i < otherPhotos.length ; i++) {
				NewsVo vo1 = new NewsVo();
				vo1.setId(news.getId());
				vo1.setImg(otherPhotos[i]);
				vo1.setIsShow(news.getIsShow());
				vo1.setKeyWords(news.getKeyWords());
				vo1.setMovie(news.getOtherMovie());
				vo1.setSort(news.getSort());
				vo1.setTitle(news.getTitle());
				vo1.setTypeId(news.getTypeId());
				if (index != 0) {
					vo1.setBeforeId(idsList.get(index-1).getId());
				}
				if (index != max -1) {
					vo1.setNextId(idsList.get(index+1).getId());
				}else{
					vo1.setNextId(idsList.get(max -1).getId());
				}
				lists.add(vo1);
			}
		}
		
		return json(lists);
	}*/
	
	
	@GetMapping("/search")
	@ApiOperation(value="搜索全部" , httpMethod = "GET" , notes ="搜索全部内容，不分类")
	public AbstractBean search(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize,@RequestParam(required = false,defaultValue="") String content){
		return json(newsService.selectMapsPage(new Page<TbNews>(pageNum, pageSize),new EntityWrapper<TbNews>().like("title", stringAddlike(content))));
	}
	
	@GetMapping("/{typeId}.search")
	@ApiOperation(value="根据TypeId搜索" , httpMethod = "GET" , notes ="分类搜索内容")
	public AbstractBean searchByTypeId(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize,@PathVariable Integer typeId,@RequestParam(required = false,defaultValue="")String content){
		return json(newsService.selectMapsPage(new Page<TbNews>(pageNum, pageSize),new EntityWrapper<TbNews>().like("title", stringAddlike(content))));
	}
	
	
/*	
	public static void main(String[] args) {
		String aString = "胡汉三可以归来了back,on";
		NewsController controller = new NewsController();
		
		System.out.println(controller.stringAddlike(aString));
		
	}*/
	
	
	public String stringAddlike(String value){
		char[] c = value.toCharArray();
		StringBuffer newvalue = new StringBuffer();
		for (int i = 0; i < c.length; i++) {
			newvalue.append("%"+c[i]);
		}	
		newvalue.append("%");
		
		return newvalue.toString();
	}
	
	
	@GetMapping("/getVedioList")
	@ApiOperation(value="获取全部视频列表" , httpMethod = "GET" , notes ="视频列表")
	public AbstractBean getVedioList(@RequestParam(required=false,defaultValue="1") Integer pageNum,@RequestParam(required=false,defaultValue="8") Integer pageSize){
		return json(newsService.selectMapsPage(new Page<TbNews>(pageNum, pageSize),new EntityWrapper<TbNews>().isNotNull("other_movie").and("other_movie != ''")));
	}
	
	@GetMapping("/getProductList/{typeId}")
	@ApiOperation(value="获取产品列表" , httpMethod = "GET" , notes ="产品列表")
	public AbstractBean getProductList(@RequestParam(required=false,defaultValue="1") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize,@RequestParam(required=true) Integer typeId){
		return json(newsService.selectMapsPage(new Page<TbNews>(pageNum, pageSize),new EntityWrapper<TbNews>().eq("type_id", typeId)));
	}
	
	/***
	 * 得到ID ，假设6，从6往后的20个
	 * @param pageNum
	 * @param pageSize
	 * @param typeId
	 * @return
	 */
	@GetMapping("/getSlideList/{typeId}/{id}")
	@ApiOperation(value="获取产品列表" , httpMethod = "GET" , notes ="产品列表")
	public AbstractBean getSlideList(@RequestParam(required=true) Integer typeId,@RequestParam(required=true) Integer id){
		return json(newsService.slideList(typeId, id));
	}
	
	/****
	 * 小图，分割后给前台
	 */
	@GetMapping("/getContent/{id}")
	@ApiOperation(value="获取产品列表" , httpMethod = "GET" , notes ="产品列表")
	public AbstractBean getContent(@RequestParam(required=true) Integer id){
		TbNews tbNews = newsService.selectById(id);
		Integer lastId = newsService.lastId(id)!= null? newsService.lastId(id) : 0;
		Integer nextId = newsService.nextId(id)!= null? newsService.nextId(id) : 0;
		NewNews newNews = new NewNews();
		newNews.setId(tbNews.getId());
		newNews.setTitle(tbNews.getTitle());
		newNews.setTypeId(tbNews.getTypeId());
		newNews.setImg0(tbNews.getCoverImg());
		newNews.setContent(tbNews.getContent());
		newNews.setLastId(lastId);
		newNews.setNextId(nextId);
		String[] list = null;
		if (tbNews.getOtherPhoto().contains(";")) {
			list = tbNews.getOtherPhoto().split(";");
			if (list.length == 1) {
				newNews.setImg1(list[0]);
			}
			if (list.length == 2) {
				newNews.setImg1(list[0]);
				newNews.setImg2(list[1]);
			}
			if (list.length == 3) {
				newNews.setImg1(list[0]);
				newNews.setImg2(list[1]);
				newNews.setImg3(list[2]);
			}
			/*if (list[0] != null && list.length >= 0) {
				newNews.setImg1(list[0]);
			}
			if (list[1] != null && list.length >=1) {
				newNews.setImg2(list[1]);
			}
			if (list[2] != null && list.length >=2) {
				newNews.setImg3(list[2]);
			}*/
		}		
		return json(newNews);
	}
	
	
}